# 爬取同花顺概念信息_ 头部已分类信息
import sys

import pymysql
import undetected_chromedriver as uc
from lxml import etree
from selenium.webdriver.common.by import By
import time
from dbse.mysql.db_mysql_insert import getDataBase
from dbse.mysql.db_mysql_insert import addThsGN
from dbse.mysql.db_mysql_insert import commonDeleteAll
from dbse.mysql.db_entity_stock_ths_gn import StockThsGN
from selenium.webdriver import ChromeOptions
from proxy.selenium_proxy_util import initChrome
from file.readFile import read
import datetime

# 环境参数 dev 开发 prod 生产
pProfile = sys.argv[1:][0]
# 初始化browser
browser = initChrome(False,False,'https://q.10jqka.com.cn/gn')

time.sleep(1)
print('====gn job begin====')
content = browser.page_source
# content = read('../file/ths_gn.txt.txt')    # 财报
html = etree.fromstring(content,etree.HTMLParser())
result = html.xpath('//div[@class="cate_items"]//a')

today = datetime.date.today().strftime("%Y-%m-%d")
db = getDataBase(pProfile)      # 通过profile 获取database
# db = pymysql.connect(host='39.98.82.109',user='root',password='landleaf@123.com',port=3306,db='stock')
sql = 'INSERT INTO stock_ths_gn (gn_code,gn_name,gn_url,create_time,gn_type,gn_desc) VALUES (%s, %s,%s,%s,%s,%s)'
deleteSql = 'delete from stock_ths_gn'

# 删除所有
commonDeleteAll(db,'stock_ths_gn')
for index, tr in enumerate(result):
    # print(tr.text)
    # print(tr.get('href'))
    splitUrlItems = tr.get('href').split("/")
    gnCode = splitUrlItems[len(splitUrlItems)-2]
    entity = StockThsGN(gnCode,tr.text,tr.get('href'),today,'1','')
    print(gnCode)
    print(tr.text)
    print(tr.get('href'))
    print(today)
    addThsGN(db,sql,entity)         # 添加概念


# elements =  browser.find_elements(By.XPATH,"//div[@class='cate_items']//a")
# for ele in elements:
#     print(ele.text)

